from rest_framework.generics import ListAPIView
from .models import SKU
from .serializers import SKUListSerializer
from meiduo_mall.utils.pagination import StandardResultsSetPagination
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet
from .serializers import SKUIndexSerializer


class SKUListView(ListAPIView):
    #查询及
    def get_queryset(self):
        #在视图中获取请求路径中的数据self.kwargs
        return SKU.objects.filter(category_id=self.kwargs['category_id'])

    #序列化器
    serializer_class = SKUListSerializer
    #分页
    pagination_class = StandardResultsSetPagination
    #排序
    filter_backends = [OrderingFilter]
    #指定排序字段
    ordering_fields = {'create_time','price','sales'}


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer

    pagination_class = StandardResultsSetPagination